import Vue from 'vue'
import Vuex from 'vuex'
import {removeLocalLoginInfor, setLocalLoginInfor, setLocalToken, setLocalUser} from "../utils/storage-utils";
import axios from "./axios";
// import {getDictsByType} from "../utils/sys-dic-utils";

Vue.use(Vuex)

export default new Vuex.Store({
    state: {
        user: {},  //roles
        token: "",
        refreshToken: "",
        tokenType: null,
        ws: {
            ww: 0,
            wh: 0,
        },  //window width 0-xs 1-sm 2-md 3-lg 4-xl
        sysDic:[],
        sysSourceIsReady:false,

        areaData: [], //地区数据
        isMapReady:false, //
        isLogin:false, //
    },
    getters: {

    },
    mutations: { // 同步函数
        loginOut(state) {
            state.token = "";
            state.user = {};
            removeLocalLoginInfor();

        },
        loginIn(state, loginResponse) {
            state.token = loginResponse.accessToken;
            state.refreshToken = loginResponse.refreshToken;
            // 把token和token过期日存入本地
            delete loginResponse.accessToken;
            delete loginResponse.refreshToken;
            delete loginResponse.tokenType;
            setLocalLoginInfor(state.token,loginResponse, new Date().getTime()+(loginResponse.expire*1000));
            state.user = loginResponse;
        },
        setLoginInfor(state,user){
            state.user = user;
            state.isLogin = true;
        },
        setUser(state,user){
            setLocalUser(user);
            state.user = user;
        },
        setToken(state,token){
            setLocalToken(token);
            state.token = token;
        },
        setSysDic(state,sysDic){
            state.sysDic = sysDic;
        },
        resizeWindow(state, windowSize) {
            state.ws = windowSize
        },
        sysSourceIsReady(state, sysSourceIsReady) {
            state.sysSourceIsReady = sysSourceIsReady
        },
        setAreaData(state, areaData) {
            state.areaData = areaData
        },

        isMapReady(state, flag) {
            state.isMapReady = flag
        },
    },
    actions: {
        areaDataAsyn(context) {
            return new Promise((resolve, reject) => {
                if(context.state.areaData.length>0){
                    resolve()
                    return;
                }
                axios.get("/sysArea/selectAll").then((rep) => {
                    if (rep.code == 1) {
                        var result = rep.result;
                        result.shift();
                        context.commit("setAreaData", result)
                        resolve()
                    }else{
                        reject()
                    }
                })

            })
        }
    },
    modules: {},
})
